const path = require('path');
const {think} = require('thinkjs');
const cors = require('koa-cors');
declare var JSON: any;
const isDev = think.env === 'development';

module.exports = [
    {
        handle: cors,
        options: {
            origin: '*'
        }
    },
    {
        handle: 'meta',
        options: {
            logRequest: true,
            sendResponseTime: true
        }
    },
    {
        handle: 'resource',
        enable: isDev,
        options: {
            root: path.join(think.ROOT_PATH, 'www'),
            publicPath: /^\/(static|favicon\.ico)/
        }
    },
    {
        handle: 'trace',
        enable: !think.isCli,
        options: {
            sourceMap: false,
            error: (error: any, ctx: any) => {
                ctx.set("Content-Type", "application/json");
                ctx.body = JSON.stringify({
                    errno: 500,
                    errmsg: error + ''
                });
                return false;

            }
        }
    },
    {
        handle: 'payload',
        options: {
            keepExtensions: true,
            limit: '5mb'
        }
    },
    {
        handle: 'router',
        options: {}
    },
    {
        handle: 'error'
    },
    /*{
        handle: 'validateSign'
    },*/
    'logic',
    'controller'
];
